package net.office.controller;

import net.office.bean.*;
import net.office.service.BaseDictService;
import net.office.service.ExpenseItemService;
import net.office.service.ExpenseService;
import net.office.service.LoginService;
import net.office.utils.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class ExpenseController {

    @Autowired
    private ExpenseService expenseService;

    @Autowired
    private ExpenseItemService expenseItemService;

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private LoginService loginService;

    /*查询个人列表*/
    @RequestMapping("/expense/person/list.do")
    public String personList(HttpSession session, Model model){
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");

        List<Expense> expenseList = expenseService.findAllByStaffId(staffInfo.getStaffId());

        for(int i=0; i<expenseList.size(); i++){
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseWayDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseWay()));
            }
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseResultDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseResult()));
            }
        }

        model.addAttribute("expenseList", expenseList);

        List<BaseDict> expenseWayList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_WAY_CODE);
        List<BaseDict> expenseResultList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_RESULT_CODE);
        model.addAttribute("expenseWayList", expenseWayList);
        model.addAttribute("expenseResultList", expenseResultList);

        return "expense";
    }

    /*搜索个人列表*/
    @RequestMapping("/expense/person/search.do")
    public String personSearch(@RequestParam(value = "expenseStaffName", required = false) String expenseStaffName,
                               @RequestParam(value = "expenseDepartment", required = false) String expenseDepartment,
                               @RequestParam(value = "expenseWay", required = false) Integer expenseWay,
                               @RequestParam(value = "expenseReceiveDepartment", required = false) String expenseReceiveDepartment,
                               @RequestParam(value = "expenseResult", required = false) Integer expenseResult,
                               HttpSession session, Model model){

        Expense expense = new Expense();
        StaffInfo staffInfo = (StaffInfo) session.getAttribute("staff");
        expense.setExpenseStaffId(staffInfo.getStaffId());

        if(expenseStaffName!=null && expenseStaffName.trim().length()!=0){
            expense.setExpenseStaffName(expenseStaffName);
        }
        if(expenseDepartment!=null && expenseDepartment.trim().length()!=0){
            expense.setExpenseDepartment(expenseDepartment);
        }
        if(expenseWay!=null){
            expense.setExpenseWay(expenseWay);
        }
        if(expenseReceiveDepartment!=null && expenseReceiveDepartment.trim().length()!=0){
            expense.setExpenseReceiveDepartment(expenseReceiveDepartment);
        }
        if(expenseResult!=null){
            expense.setExpenseResult(expenseResult);
        }

        List<Expense> expenseList = expenseService.findByExpense(expense);

        for(int i=0; i<expenseList.size(); i++){
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseWayDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseWay()));
            }
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseResultDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseResult()));
            }
        }

        model.addAttribute("expenseList", expenseList);

        List<BaseDict> expenseWayList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_WAY_CODE);
        List<BaseDict> expenseResultList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_RESULT_CODE);
        model.addAttribute("expenseWayList", expenseWayList);
        model.addAttribute("expenseResultList", expenseResultList);

        return "expense";
    }

    /*查询全部列表*/
    @RequestMapping("/expense/all/list.do")
    public String allList(Model model){
        List<Expense> expenseList = expenseService.findAll();

        for(int i=0; i<expenseList.size(); i++){
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseWayDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseWay()));
            }
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseResultDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseResult()));
            }
        }

        model.addAttribute("expenseList", expenseList);

        List<BaseDict> expenseWayList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_WAY_CODE);
        List<BaseDict> expenseResultList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_RESULT_CODE);
        model.addAttribute("expenseWayList", expenseWayList);
        model.addAttribute("expenseResultList", expenseResultList);

        return "expense";
    }

    /*搜索全部列表*/
    @RequestMapping("/expense/all/search.do")
    public String allSearch(@RequestParam(value = "expenseStaffName", required = false) String expenseStaffName,
                            @RequestParam(value = "expenseDepartment", required = false) String expenseDepartment,
                            @RequestParam(value = "expenseWay", required = false) Integer expenseWay,
                            @RequestParam(value = "expenseReceiveDepartment", required = false) String expenseReceiveDepartment,
                            @RequestParam(value = "expenseResult", required = false) Integer expenseResult,
                            Model model){

        Expense expense = new Expense();

        if(expenseStaffName!=null && expenseStaffName.trim().length()!=0){
            expense.setExpenseStaffName(expenseStaffName);
        }
        if(expenseDepartment!=null && expenseDepartment.trim().length()!=0){
            expense.setExpenseDepartment(expenseDepartment);
        }
        if(expenseWay!=null){
            expense.setExpenseWay(expenseWay);
        }
        if(expenseReceiveDepartment!=null && expenseReceiveDepartment.trim().length()!=0){
            expense.setExpenseReceiveDepartment(expenseReceiveDepartment);
        }
        if(expenseResult!=null){
            expense.setExpenseResult(expenseResult);
        }

        List<Expense> expenseList = expenseService.findByExpense(expense);

        for(int i=0; i<expenseList.size(); i++){
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseWayDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseWay()));
            }
            if(expenseList.get(i).getExpenseWay() != null) {
                expenseList.get(i).setExpenseResultDict(baseDictService.findOneByDictId(expenseList.get(i).getExpenseResult()));
            }
        }

        model.addAttribute("expenseList", expenseList);

        List<BaseDict> expenseWayList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_WAY_CODE);
        List<BaseDict> expenseResultList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_RESULT_CODE);
        model.addAttribute("expenseWayList", expenseWayList);
        model.addAttribute("expenseResultList", expenseResultList);

        return "expense";
    }

    /*新增*/
    @RequestMapping("/expense/insert.do")
    public String insert(@RequestParam(value = "expenseDepartment", required = false) String expenseDepartment,
                         @RequestParam(value = "expenseDealName", required = false) String expenseDealName,
                         @RequestParam(value = "expenseItemType", required = false) String[] expenseItemType,
                         @RequestParam(value = "expenseItemName", required = false) String[] expenseItemName,
                         @RequestParam(value = "expenseItemUnitPrice", required = false) Double[] expenseItemUnitPrice,
                         @RequestParam(value = "expenseItemNum", required = false) Integer[] expenseItemNum,
                         @RequestParam(value = "expenseItemTotalPrice", required = false) Double[] expenseItemTotalPrice,
                         @RequestParam(value = "expenseTotal", required = false) Double expenseTotal,
                         @RequestParam(value = "expenseWay", required = false) Integer expenseWay,
                         @RequestParam(value = "expenseReceiveDepartment", required = false) String expenseReceiveDepartment,
                         @RequestParam(value = "expenseReceiveAccount", required = false) String expenseReceiveAccount,
                         @RequestParam(value = "expenseReceiveBank", required = false) String expenseReceiveBank,
                         @RequestParam(value = "generalManagerStaffName", required = false) String generalManagerStaffName,
                         @RequestParam(value = "departManagerStaffName", required = false) String departManagerStaffName,
                         @RequestParam(value = "auditorStaffName", required = false) String auditorStaffName,
                         @RequestParam(value = "expenseStaffNo", required = false) String expenseStaffNo,
                         @RequestParam(value = "expenseStaffName", required = false) String expenseStaffName,
                         @RequestParam(value = "expenseResult", required = false) Integer expenseResult,
                         HttpSession session, Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(expenseStaffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/expense/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(expenseStaffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/expense/toAdd.do";
        }

        Expense expense = new Expense();

        expense.setExpenseStaffId(staffInfo.getStaffId());
        expense.setExpenseStaffNo(expenseStaffNo);
        expense.setExpenseStaffName(expenseStaffName);

        if(expenseDepartment!=null && expenseDepartment.trim().length()!=0){
            expense.setExpenseDepartment(expenseDepartment);
        }
        if(expenseDealName!=null && expenseDealName.trim().length()!=0){
            expense.setExpenseDealName(expenseDealName);
        }
        if(expenseTotal!=null){
            expense.setExpenseTotal(expenseTotal);
        }
        if(expenseWay!=null){
            expense.setExpenseWay(expenseWay);
        }
        if(expenseReceiveDepartment!=null && expenseReceiveDepartment.trim().length()!=0){
            expense.setExpenseReceiveDepartment(expenseReceiveDepartment);
        }
        if(expenseReceiveAccount!=null && expenseReceiveAccount.trim().length()!=0){
            expense.setExpenseReceiveAccount(expenseReceiveAccount);
        }
        if(expenseReceiveBank!=null && expenseReceiveBank.trim().length()!=0){
            expense.setExpenseReceiveBank(expenseReceiveBank);
        }
        if(generalManagerStaffName!=null && generalManagerStaffName.trim().length()!=0){
            expense.setGeneralManagerStaffName(generalManagerStaffName);
        }
        if(departManagerStaffName!=null && departManagerStaffName.trim().length()!=0){
            expense.setDepartManagerStaffName(departManagerStaffName);
        }
        if(auditorStaffName!=null && auditorStaffName.trim().length()!=0){
            expense.setAuditorStaffName(auditorStaffName);
        }
        if(expenseResult!=null){
            expense.setExpenseResult(expenseResult);
        }

        int expense_res = expenseService.insert(expense);

        //expenseItem
        if(expense_res > 0) {
            List<ExpenseItem> expenseItemList = new ArrayList<ExpenseItem>();
            int i = 0;
            while (i < expenseItemType.length || i < expenseItemName.length || i < expenseItemUnitPrice.length
                    || i < expenseItemNum.length || i < expenseItemTotalPrice.length) {

                ExpenseItem expenseItem = new ExpenseItem();
                expenseItem.setExpenseId(expense.getExpenseId());

                if (i < expenseItemType.length) {
                    expenseItem.setExpenseItemType(expenseItemType[i]);
                }
                if (i < expenseItemName.length) {
                    expenseItem.setExpenseItemName(expenseItemName[i]);
                }
                if (i < expenseItemUnitPrice.length) {
                    expenseItem.setExpenseItemUnitPrice(expenseItemUnitPrice[i]);
                }
                if (i < expenseItemNum.length) {
                    expenseItem.setExpenseItemNum(expenseItemNum[i]);
                }
                if (i < expenseItemTotalPrice.length) {
                    expenseItem.setExpenseItemTotalPrice(expenseItemTotalPrice[i]);
                }

                expenseItemList.add(expenseItem);
                i++;
            }
            int item_res = expenseItemService.updateList(expenseItemList);
        }

        if(expense_res > 0) {
            if(true) {
                return "redirect:/expense/all/list.do";
            }else {
                return "redirect:/expense/person/list.do";
            }
        }


        return "expense-add";
    }

    /*修改*/
    @RequestMapping("/expense/update.do")
    public String update(@RequestParam(value = "expenseId") Integer expenseId,
                         @RequestParam(value = "expenseDepartment", required = false) String expenseDepartment,
                         @RequestParam(value = "expenseDealName", required = false) String expenseDealName,
                         @RequestParam(value = "expenseItemId", required = false) Integer[] expenseItemId,
                         @RequestParam(value = "expenseItemType", required = false) String[] expenseItemType,
                         @RequestParam(value = "expenseItemName", required = false) String[] expenseItemName,
                         @RequestParam(value = "expenseItemUnitPrice", required = false) Double[] expenseItemUnitPrice,
                         @RequestParam(value = "expenseItemNum", required = false) Integer[] expenseItemNum,
                         @RequestParam(value = "expenseItemTotalPrice", required = false) Double[] expenseItemTotalPrice,
                         @RequestParam(value = "expenseTotal", required = false) Double expenseTotal,
                         @RequestParam(value = "expenseWay", required = false) Integer expenseWay,
                         @RequestParam(value = "expenseReceiveDepartment", required = false) String expenseReceiveDepartment,
                         @RequestParam(value = "expenseReceiveAccount", required = false) String expenseReceiveAccount,
                         @RequestParam(value = "expenseReceiveBank", required = false) String expenseReceiveBank,
                         @RequestParam(value = "generalManagerStaffName", required = false) String generalManagerStaffName,
                         @RequestParam(value = "departManagerStaffName", required = false) String departManagerStaffName,
                         @RequestParam(value = "auditorStaffName", required = false) String auditorStaffName,
                         @RequestParam(value = "expenseStaffNo", required = false) String expenseStaffNo,
                         @RequestParam(value = "expenseStaffName", required = false) String expenseStaffName,
                         @RequestParam(value = "expenseResult", required = false) Integer expenseResult,
                         Model model){

        //检查员工是否存在，工号、姓名是否保持一致,...
        StaffInfo staffInfo = loginService.findByStaffNo(expenseStaffNo);
        if(staffInfo == null){
            //model.addAttribute("msg", "staff");
            return "redirect:/expense/toAdd.do";
        }else if(!staffInfo.getStaffName().equals(expenseStaffName)){
            //model.addAttribute("msg", "staff");
            return "redirect:/expense/toAdd.do";
        }

        Expense expense = new Expense();

        expense.setExpenseId(expenseId);
        expense.setExpenseStaffId(staffInfo.getStaffId());
        expense.setExpenseStaffNo(expenseStaffNo);
        expense.setExpenseStaffName(expenseStaffName);

        if(expenseDepartment!=null && expenseDepartment.trim().length()!=0){
            expense.setExpenseDepartment(expenseDepartment);
        }
        if(expenseDealName!=null && expenseDealName.trim().length()!=0){
            expense.setExpenseDealName(expenseDealName);
        }
        if(expenseTotal!=null){
            expense.setExpenseTotal(expenseTotal);
        }
        if(expenseWay!=null){
            expense.setExpenseWay(expenseWay);
        }
        if(expenseReceiveDepartment!=null && expenseReceiveDepartment.trim().length()!=0){
            expense.setExpenseReceiveDepartment(expenseReceiveDepartment);
        }
        if(expenseReceiveAccount!=null && expenseReceiveAccount.trim().length()!=0){
            expense.setExpenseReceiveAccount(expenseReceiveAccount);
        }
        if(expenseReceiveBank!=null && expenseReceiveBank.trim().length()!=0){
            expense.setExpenseReceiveBank(expenseReceiveBank);
        }
        if(generalManagerStaffName!=null && generalManagerStaffName.trim().length()!=0){
            expense.setGeneralManagerStaffName(generalManagerStaffName);
        }
        if(departManagerStaffName!=null && departManagerStaffName.trim().length()!=0){
            expense.setDepartManagerStaffName(departManagerStaffName);
        }
        if(auditorStaffName!=null && auditorStaffName.trim().length()!=0){
            expense.setAuditorStaffName(auditorStaffName);
        }
        if(expenseResult!=null){
            expense.setExpenseResult(expenseResult);
        }

        int expense_res = expenseService.updateByPrimaryKey(expense);

        //expenseItem
        if(expense_res > 0) {
            List<ExpenseItem> expenseItemList = new ArrayList<ExpenseItem>();
            int i = 0;
            while (i < expenseItemId.length || i < expenseItemType.length || i < expenseItemName.length || i < expenseItemUnitPrice.length
                    || i < expenseItemNum.length || i < expenseItemTotalPrice.length) {

                ExpenseItem expenseItem = new ExpenseItem();
                expenseItem.setExpenseId(expense.getExpenseId());

                if (i < expenseItemId.length) {
                    expenseItem.setExpenseItemId(expenseItemId[i]);
                }
                if (i < expenseItemType.length) {
                    expenseItem.setExpenseItemType(expenseItemType[i]);
                }
                if (i < expenseItemName.length) {
                    expenseItem.setExpenseItemName(expenseItemName[i]);
                }
                if (i < expenseItemUnitPrice.length) {
                    expenseItem.setExpenseItemUnitPrice(expenseItemUnitPrice[i]);
                }
                if (i < expenseItemNum.length) {
                    expenseItem.setExpenseItemNum(expenseItemNum[i]);
                }
                if (i < expenseItemTotalPrice.length) {
                    expenseItem.setExpenseItemTotalPrice(expenseItemTotalPrice[i]);
                }

                expenseItemList.add(expenseItem);
                i++;
            }
            int item_res = expenseItemService.updateList(expenseItemList);
        }

        if(expense_res > 0) {

        }

        return "redirect:/expense/toEdit.do?expenseId="+expenseId+"";
    }

    /*删除单条(ajax)*/
    @RequestMapping("/expense/deleteOne.do")
    @ResponseBody
    public Object deleteOne(@RequestParam("expenseId") Integer expenseId){

        int expense_res = expenseService.deleteByPrimaryKey(expenseId);
        int item_res = expenseItemService.deleteByExpenseId(expenseId);

        Map<String, String> map = new HashMap<String, String>();
        if(expense_res > 0){
            map.put("res", "1");
        }else{
            map.put("res", "0");
        }
        return map;
    }

    /*跳转到新增页面*/
    @RequestMapping("/expense/toAdd.do")
    public String toAdd(Model model){

        List<BaseDict> expenseWayList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_WAY_CODE);
        List<BaseDict> expenseResultList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_RESULT_CODE);
        model.addAttribute("expenseWayList", expenseWayList);
        model.addAttribute("expenseResultList", expenseResultList);

        return "expense-add";
    }
    /*跳转到修改页面*/
    @RequestMapping("/expense/toEdit.do")
    public String toEdit(@RequestParam("expenseId") Integer expenseId, Model model){
        Expense expense = expenseService.findOneByExpenseId(expenseId);
        model.addAttribute("expense", expense);

        List<ExpenseItem> expenseItemList = expenseItemService.findByExpenseId(expenseId);
        model.addAttribute("expenseItemList", expenseItemList);

        List<BaseDict> expenseWayList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_WAY_CODE);
        List<BaseDict> expenseResultList = baseDictService.findByDictTypeCode(TemplateParam.EXPENSE_RESULT_CODE);
        model.addAttribute("expenseWayList", expenseWayList);
        model.addAttribute("expenseResultList", expenseResultList);

        return "expense-edit";
    }
}
